/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mapfinal.portal.web.tag;

import java.io.IOException;
import java.util.Map;

import com.mapfinal.portal.model.Article;
import com.mapfinal.portal.service.ArticleService;
import com.lambkit.common.util.StringUtils;
import com.lambkit.common.service.ServiceKit;
import com.lambkit.web.tag.base.BaseDirectiveModel;
import com.jfinal.kit.StrKit;
import com.jfinal.render.FreeMarkerRender;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2019-05-23
 * @version 1.0
 * @since 1.0
 */
/**
 * cms_article标签<br>
 * 参数：{id:主键}
 * 返回值：{entity:cms_article信息}
 * @author lambkit
 */
public class ArticleDirective extends BaseDirectiveModel {

	@Override
	public void executeMe(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		// TODO Auto-generated method stub
		String id = get(params, "id");
		String pid = get(params, "pid");
		String slug = get(params, "slug");
		String title = get(params, "title");
		String content = get(params, "content");
		String editMode = get(params, "edit_mode");
		String summary = get(params, "summary");
		String linkTo = get(params, "link_to");
		String fromurl = get(params, "fromurl");
		String thumbnail = get(params, "thumbnail");
		String style = get(params, "style");
		String userId = get(params, "user_id");
		String orderNumber = get(params, "order_number");
		String status = get(params, "status");
		String commentStatus = get(params, "comment_status");
		String commentCount = get(params, "comment_count");
		String commentTime = get(params, "comment_time");
		String viewCount = get(params, "view_count");
		String created = get(params, "created");
		String modified = get(params, "modified");
		String flag = get(params, "flag");
		String metaKeywords = get(params, "meta_keywords");
		String metaDescription = get(params, "meta_description");
		String remarks = get(params, "remarks");
		int pagenum = getInt(params, "pagenum", 0);
		int pagesize = getInt(params, "pagesize", 0);
		String wheresql = get(params, "sql", null);
		String sql = " from cms_article where "; 
		if(wheresql == null) {
			sql += " 1=1 ";
			if(StringUtils.hasText(id)) sql += " and id like '%" + id + "%'";//bigserial
			if(StringUtils.hasText(pid)) sql += " and pid=" + pid;//int4
			if(StringUtils.hasText(slug)) sql += " and slug like '%" + slug + "%'";//varchar
			if(StringUtils.hasText(title)) sql += " and title like '%" + title + "%'";//varchar
			if(StringUtils.hasText(content)) sql += " and content like '%" + content + "%'";//text
			if(StringUtils.hasText(editMode)) sql += " and edit_mode like '%" + editMode + "%'";//varchar
			if(StringUtils.hasText(summary)) sql += " and summary like '%" + summary + "%'";//text
			if(StringUtils.hasText(linkTo)) sql += " and link_to like '%" + linkTo + "%'";//varchar
			if(StringUtils.hasText(fromurl)) sql += " and fromurl like '%" + fromurl + "%'";//varchar
			if(StringUtils.hasText(thumbnail)) sql += " and thumbnail like '%" + thumbnail + "%'";//varchar
			if(StringUtils.hasText(style)) sql += " and style like '%" + style + "%'";//varchar
			if(StringUtils.hasText(userId)) sql += " and user_id=" + userId;//int8
			if(StringUtils.hasText(orderNumber)) sql += " and order_number=" + orderNumber;//int8
			if(StringUtils.hasText(status)) sql += " and status like '%" + status + "%'";//varchar
			if(StringUtils.hasText(commentStatus)) sql += " and comment_status=" + commentStatus;//int2
			if(StringUtils.hasText(commentCount)) sql += " and comment_count=" + commentCount;//int8
			if(StringUtils.hasText(commentTime)) sql += " and comment_time like '%" + commentTime + "%'";//timestamp
			if(StringUtils.hasText(viewCount)) sql += " and view_count=" + viewCount;//int8
			if(StringUtils.hasText(created)) sql += " and created like '%" + created + "%'";//timestamp
			if(StringUtils.hasText(modified)) sql += " and modified like '%" + modified + "%'";//timestamp
			if(StringUtils.hasText(flag)) sql += " and flag like '%" + flag + "%'";//varchar
			if(StringUtils.hasText(metaKeywords)) sql += " and meta_keywords like '%" + metaKeywords + "%'";//varchar
			if(StringUtils.hasText(metaDescription)) sql += " and meta_description like '%" + metaDescription + "%'";//varchar
			if(StringUtils.hasText(remarks)) sql += " and remarks like '%" + remarks + "%'";//text
		} else {
			sql += wheresql;
		}
		
		String orderby = get(params, "orderby", null);
		
		if(StrKit.notBlank(orderby)) {
			sql += " order by " + orderby;
		}
		
		ArticleService service = Article.service();
		
		String tagEntityKeyname = get(params, "key", "entity");
		if(pagenum==0) {
			env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(
					service.dao().findFirst("select *" + sql)));
		} else {
			if(pagesize==0) {
				env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(
						service.dao().find("select *" + sql)));
			} else {
				env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(
						service.dao().paginate(pagenum, pagesize, "select *", sql)));
			}
		}
        body.render(env.getOut());
	}
}
